<html>
	<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&language=ko"></script>
	<script src="/~dbdbdib/static/lib/jquery-1.7.1.min.js"></script>
	<script type="text/javascript">
		var data = <?=json_encode($arr)?>;
		var geocoder = new google.maps.Geocoder();
		var responses = new Array();
		var errors = new Array();
		
		var t = setInterval(function(){
			$('body').prop('scrollTop', $('body').prop('scrollHeight'));
		}, 10);
		
		function geocode(idx){
			if( idx >= data.length){
				console.log("done!");
				clearInterval(t);
				return;
			}
			if(! data[idx] || !data[idx].name_full){
				console.log("error: " + idx);
				geocode(idx+1);
			}
				
			geocoder.geocode({
				address : data[idx].name_full
			}, function(res){
				if(res && res.length > 0 && res[0].geometry && res[0].geometry.location) {
					var loc = res[0].geometry.location;
					data[idx].lat = loc.lat();
					data[idx].lng = loc.lng();
					responses[idx] = res;
					$('body').append("('" + data[idx].id + "','" + data[idx].level + "','" + data[idx].name+ "','" + data[idx].belongs_to + "','" + data[idx].lat+ "','" + data[idx].lng +  "')" + ((idx+1)<data.length ? ',':''));
					console.log('success: ' + data[idx].name_full + "("+ (idx+1)+" of "+ data.length +")");
				}else{
					errors[idx] = {
						data: data[idx], 
						res : res
					};
					console.log('failed: ' + data[idx].name_full + "("+ (idx+1)+" of "+ data.length +")");
				}
				setTimeout(function(){
					geocode(idx+1);
				}, 1000);
			});
		}
			
		
	</script>
	<body>
		INSERT INTO address(`id`, `level`, `name`, `belongs_to`, `lat`, `lng`) VALUES<br>
	</body>
</html>